package com.java.example.repository

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.java.example.repository.dao.ProjectLocalListBeanDao
import com.java.example.repository.entity.ProjectLocalBeanList


/**
 * @author chenll
 * @date 2024/5/17
 */

@Database(
        version = 1,
        entities = [ProjectLocalBeanList::class],
        exportSchema = false
)
abstract class MyDataBase : RoomDatabase() {
    companion object {
        private var db: MyDataBase? = null
        private val name = "notes-db"
        fun getDB(context: Context) =
            synchronized(MyDataBase::class) {
                if (db == null) {
                    Room.databaseBuilder(context, MyDataBase::class.java, name)
                        .enableMultiInstanceInvalidation()
                        .allowMainThreadQueries()//允许主线程操作数据库，不建议开启
//                        .addMigrations(MIGRATION_1_2)//数据库版本切换
                        .build().apply { db = this }
                } else {
                    db!!
                }
            }

        val MIGRATION_1_2 = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("DELETE FROM mapSearchEntity")
            }
        }
    }

    abstract fun getProjectLocalListBeanDao(): ProjectLocalListBeanDao

}